package com.meiyu.live.tv.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.meiyu.live.R;
import com.meiyu.live.util.DisplayUtils;

public class DanmuSwitch extends View {

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap bitmap_switch_on, bitmap_switch_off;

    private RectF rectF = new RectF();

    private boolean isChecked = false;

    private OnDanmuSwitchChangeListener listener;

    public interface OnDanmuSwitchChangeListener {
        public void onCheckedChanged(boolean isChecked);
    }

    public DanmuSwitch(Context context) {
        super(context);
        init(context);
    }

    public DanmuSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        bitmap_switch_on = BitmapFactory.decodeResource(getResources(), R.drawable.switch_danmu_on);
        bitmap_switch_off = BitmapFactory.decodeResource(getResources(), R.drawable.switch_danmu_off);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isChecked = !isChecked;
                postInvalidate();
                if (listener != null) {
                    listener.onCheckedChanged(isChecked);
                }
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = isChecked ? bitmap_switch_on : bitmap_switch_off;
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        int width = getWidth();
        int height = getHeight();

        int padding = DisplayUtils.dpToPixel(getContext(), 2);

        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(0xffdcdcdc);
        rectF.set(0, 0, width, height);
        canvas.drawRoundRect(rectF, height / 2, height / 2, paint);

        Matrix matrix = canvas.getMatrix();
        matrix.reset();
        float scaleY = (float) (height - padding * 2) / (float) bitmap.getHeight();
        matrix.postScale(scaleY, scaleY);
        matrix.postTranslate(isChecked ? width - padding - bitmap.getWidth() * scaleY : padding, padding);
        canvas.drawBitmap(bitmap, matrix, null);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (bitmap_switch_on != null && !bitmap_switch_on.isRecycled()) {
            bitmap_switch_on.recycle();
            bitmap_switch_on = null;
        }
        if (bitmap_switch_off != null && !bitmap_switch_off.isRecycled()) {
            bitmap_switch_off.recycle();
            bitmap_switch_off = null;
        }
    }

    public void setOnDanmuSwitchChangeListener(OnDanmuSwitchChangeListener listener) {
        this.listener = listener;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        postInvalidate();
    }

    public boolean getChecked() {
        return isChecked;
    }
}